
#import "PUPDLunBoView.h"
#import "UIImageView+WebCache.h"
#import "PUPDLunBoCell.h"
#define  YYIDCell @"CollectionViewCell"
#define YYMaxSections _window_height
@interface PUPDLunBoView ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UIColor *color;
    NSTimer *timers;
}
@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic , strong) UIPageControl *pageControl;
@property (nonatomic , strong) NSArray *newses;
@property (nonatomic , strong) NSTimer *timer;
@property(nonatomic)int isXQ;
@property(nonatomic)CGFloat w;
@property(nonatomic)CGFloat h;

@property (nonatomic, strong)NSMutableArray<UIImageView *> *imageViewArrM;
@property (nonatomic, strong)UIButton *editImageBtn;//点击修改头像
@property (nonatomic, strong)UIView *headbgView;//多个头像的底图
@property (nonatomic, strong)UIImageView *tempImage;
@property (nonatomic, strong)UIView *tempView;
@property (nonatomic,retain)NSMutableArray *headData;


@end
@implementation PUPDLunBoView
- (NSMutableArray<UIImageView *> *)imageViewArrM {
    if (_imageViewArrM == nil) {
        _imageViewArrM = [NSMutableArray array];
    }
    return _imageViewArrM;
}
-(void)drawRect:(CGRect)rect{
    color = [UIColor colorWithRed:237/255.0 green:245/255.0 blue:244/255.0 alpha:1];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 10);
    CGContextSetStrokeColorWithColor(ctx, color.CGColor);
    CGContextMoveToPoint(ctx, 0, 120);
    CGContextAddLineToPoint(ctx, self.frame.size.width,120);
    CGContextStrokePath(ctx);
}-(instancetype)init{
    self = [super init];
    if (self) {
        [self initView];
    }
    return self;
}
-(void)reload:(NSArray *)list{
    
    NSMutableArray *l=[NSMutableArray array];
    for (int i=0; i<list.count; i++) {
        if (i<5) {
            [l addObject:list[i]];
        }
    }
    
    _newses = list;
    _newses = l;
    CGFloat w =  _window_width;
    self.frame = CGRectMake(0, 0,w,w);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(w,w);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, w,w) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.pagingEnabled = YES;
    collectionView.backgroundColor = [UIColor colorWithHex:0xFAFCFF];
    collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView=collectionView;
    //    [self.collectionView registerNib:[UINib nibWithNibName:@"lunbocell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
    [self.collectionView registerClass:[PUPDLunBoCell class] forCellWithReuseIdentifier:@"YDCollectionViewCell"];
#pragma mark -- pageControll
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    
    //    pageControl.center =CGPointMake(self.center.x, 155);
    pageControl.pageIndicatorTintColor = [UIColor colorWithRed:216/255.0 green:216/255.0 blue:216/255.0 alpha:0.6] ;
    pageControl.currentPageIndicatorTintColor =[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1];
    pageControl.enabled = NO;
    pageControl.numberOfPages = _newses.count;
    
    _pageControl=pageControl;
    [self addSubview:self.collectionView];
    [self addSubview:pageControl];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.collectionView);
        make.bottom.equalTo(self.collectionView).offset(-30);
    }];
    
    [self.collectionView reloadData];
//    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:_window_height/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
//    [self addTimer];
    self.pageControl.numberOfPages = _newses.count;
    if (_newses.count==1) {
        _pageControl.hidden=YES;
        collectionView.scrollEnabled=NO;
    }
    
//    [self addSubview:self.editImageBtn];
//    [self addSubview:self.headbgView];
//    [self.headbgView removeAllSubviews];
//    //移除所有标签image
//    for (UIImageView *image in self.imageViewArrM) {
//        [image removeFromSuperview];
//    }
//
//    self.headData=[NSMutableArray arrayWithArray:list];
//
//    NSUInteger count=self.headData.count;
//    if (count>4) {
//        count=5;
//    }
//    self.headbgView.frame=CGRectMake(self.frame.size.width-(37*KWidthScale+4*KWidthScale)*count-12*KWidthScale, 297*KWidthScale, (37*KWidthScale+4*KWidthScale)*self.headData.count, 37*KWidthScale);
//    for (int i=0; i<self.headData.count; i++) {
//        if (i>4) {
//            break;
//        }
//        UIImageView *images=[[UIImageView alloc]init];
////        images.image=[UIImage imageNamed:self.headData[i]];
//        [images sd_setImageWithURL:[NSURL URLWithString:self.headData[i]]];
//        images.frame=CGRectMake((37*KWidthScale+4*KWidthScale)*i, 0, 37*KWidthScale, 37*KWidthScale);
//        images.layer.masksToBounds=YES;
//        images.layer.cornerRadius = 3*KWidthScale;
//        images.tag=i;
//        images.alpha=0.6;
//        if(i==0){
//            images.layer.borderColor = [UIColor whiteColor].CGColor;
//            images.layer.borderWidth = 1.0;
//            self.tempImage=images;
//            images.alpha=1;
//        }
//        images.backgroundColor=[UIColor colorWithHex:0xF2F2F2];
//        images.userInteractionEnabled=YES;
//        images.contentMode=UIViewContentModeScaleAspectFill;
//        UITapGestureRecognizer *imagesTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagesClick:)];
//        [images addGestureRecognizer:imagesTap];
//        [self.headbgView addSubview:images];
//        [self.imageViewArrM addObject:images];
//    }
//
//    if (self.isSelf==1) {
//        self.editImageBtn.frame = CGRectMake(self.headbgView.frame.origin.x-(37*KWidthScale+4*KWidthScale),  297*KWidthScale, 37*KWidthScale, 37*KWidthScale);
//    }
}
-(void)imagesClick:(UITapGestureRecognizer*)tap{
    UIImageView *tableGridImage = (UIImageView*)tap.view;
    NSInteger index = tableGridImage.tag;
    NSLog(@"%ld",(long)index);
    if (self.tempImage==tableGridImage) {
        return;
    }
    
    self.tempImage.alpha=0.6;
    self.tempImage.layer.borderColor = [UIColor clearColor].CGColor;
    self.tempImage.layer.borderWidth = 0.0;
    self.tempImage=tableGridImage;
    self.tempImage.alpha=1;
    self.tempImage.layer.borderColor = [UIColor whiteColor].CGColor;
    self.tempImage.layer.borderWidth = 1.0;
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
 
}

-(void)reload:(NSArray *)list frame:(CGRect)frame{
    _newses = list;
    self.isXQ=1;
    CGFloat w =  frame.size.width;
    CGFloat h =  frame.size.height;
    self.w=w;
    self.h=h;
    self.frame = CGRectMake(0, 0,w,h);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(w,h);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, w,h) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.pagingEnabled = YES;
    collectionView.backgroundColor = [UIColor colorWithHex:0xFAFCFF];
    _collectionView=collectionView;
    //    [self.collectionView registerNib:[UINib nibWithNibName:@"lunbocell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
    [self.collectionView registerClass:[PUPDLunBoCell class] forCellWithReuseIdentifier:@"YDCollectionViewCell"];
#pragma mark -- pageControll
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    
    //    pageControl.center =CGPointMake(self.center.x, 155);
    pageControl.pageIndicatorTintColor = [UIColor colorWithRed:216/255.0 green:216/255.0 blue:216/255.0 alpha:0.6] ;
    pageControl.currentPageIndicatorTintColor =[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1];
    pageControl.enabled = NO;
    pageControl.numberOfPages = _newses.count;
    
    _pageControl=pageControl;
    [self addSubview:self.collectionView];
    [self addSubview:pageControl];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.collectionView);
        make.bottom.equalTo(self.collectionView).offset(-4);
    }];
    
    [self.collectionView reloadData];
//    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:_window_height/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
//    [self addTimer];
    self.pageControl.numberOfPages = _newses.count;
    if (_newses.count==1) {
        _pageControl.hidden=YES;
        collectionView.scrollEnabled=NO;
    }
    
}


- (void)initView{
    
    CGFloat w =  _window_width;
    self.frame = CGRectMake(0, 0,w,w);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(w,w);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, w,w) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.pagingEnabled = YES;
    collectionView.backgroundColor = [UIColor colorWithHex:0xFAFCFF];
    collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView=collectionView;
//    [self.collectionView registerNib:[UINib nibWithNibName:@"lunbocell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
    [self.collectionView registerClass:[PUPDLunBoCell class] forCellWithReuseIdentifier:@"YDCollectionViewCell"];
#pragma mark -- pageControll
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    
//    pageControl.center =CGPointMake(self.center.x, 155);
    pageControl.pageIndicatorTintColor = [UIColor colorWithRed:216/255.0 green:216/255.0 blue:216/255.0 alpha:0.6] ;
    pageControl.currentPageIndicatorTintColor =[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1];
    pageControl.enabled = NO;
    pageControl.numberOfPages = _newses.count;
    
    _pageControl=pageControl;
    [self addSubview:self.collectionView];
    [self addSubview:pageControl];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.collectionView);
        make.bottom.equalTo(self.collectionView).offset(-4);
    }];
    
    
    [self addSubview:self.headbgView];
    [self.headbgView removeAllSubviews];
}
-(UIView*)headbgView{
    if (!_headbgView) {
        _headbgView=[[UIView alloc]init];
//        _headbgView.backgroundColor=[UIColor colorWithHex:0xFAFCFF];
    }
    return _headbgView;
}
- (UIButton *)editImageBtn {
    if (!_editImageBtn) {
        _editImageBtn = [UIButton new];
        [_editImageBtn setImage:[UIImage imageNamed:@"PDBJTP"] forState:UIControlStateNormal];
        [_editImageBtn addTarget:self action:@selector(editImageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editImageBtn;
}
-(void)editImageBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(CPPDLunBoViewEditImageBtnClick)])
    {
        [self.delegate CPPDLunBoViewEditImageBtnClick];
    }
}
#pragma mark 添加定时器
-(void) addTimer{
    if (self.newses.count >1) {
        if (!timers) {
            
        timers = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextpage) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:timers forMode:NSRunLoopCommonModes];
        
        self.timer = timers ;
       }
    }else{
        _pageControl.hidden=YES;
    }
}
#pragma mark 删除定时器
-(void) removeTimer{
    
    [self.timer invalidate];
    self.timer = nil;
    
}
-(void) nextpage{
    NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
    NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:_window_height/2];
    [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    NSInteger nextItem = currentIndexPathReset.item +1;
    NSInteger nextSection = currentIndexPathReset.section;
    if (nextItem==self.newses.count) {
        nextItem=0;
        nextSection++;
    }
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return YYMaxSections;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    [self.delegate jumpWebView:[_newses[indexPath.row] valueForKey:@"slide_url"]];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(CPPDLunBoViewClick:)])
    {
        [self.delegate CPPDLunBoViewClick:@""];
    }
    NSLog(@"%ld",(long)indexPath.item);
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.newses.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PUPDLunBoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YDCollectionViewCell" forIndexPath:indexPath];
    NSString *imageStr =  self.newses[indexPath.row];
    cell.imageviewsss.clipsToBounds = YES;
    cell.imageviewsss.backgroundColor = [UIColor colorWithHex:0xFAFCFF];
    if (self.isSJEnter==1) {
//        cell.imageviewsss.image=(UIImage*)imageStr;
        [cell.imageviewsss sd_setImageWithURL:[NSURL URLWithString:imageStr]];
    }else{
        if(self.ispng==1){
            cell.imageviewsss.image=[UIImage imageNamed:imageStr];
//            [cell.imageviewsss sd_setImageWithURL:[NSURL URLWithString:imageStr]];
        }else{
            [cell.imageviewsss sd_setImageWithURL:[NSURL URLWithString:imageStr]];
        }
    }
    
    
//    cell.imageviewsss.image=[UIImage imageNamed:self.newses[indexPath.row]];
//    [cell.imageviewsss sd_setImageWithURL:[NSURL URLWithString:[self.newses[indexPath.row] valueForKey:@"slide_pic"]] placeholderImage:[UIImage imageNamed:@"mr.png"]];
    if (self.isXQ==1) {
        cell.contentView.frame = CGRectMake(0, 0, self.w, self.h);
        cell.imageviewsss.frame = CGRectMake(0, 0, self.w, self.h);
        cell.imageviewsss.contentMode = UIViewContentModeScaleAspectFill;
    }else{
        cell.contentView.frame = CGRectMake(0, 0, _window_width, _window_width);
        cell.imageviewsss.contentMode = UIViewContentModeScaleAspectFit;
    }
    cell.imageviewsss.contentMode = UIViewContentModeScaleAspectFill;
    return cell;
}
//-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    [self removeTimer];
//}
#pragma mark 当用户停止的时候调用
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    [self addTimer];
}
#pragma mark 设置页码
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = (int) (scrollView.contentOffset.x/scrollView.frame.size.width+0.5)%self.newses.count;
    self.pageControl.currentPage =page;
    
//    UIImageView *tableGridImage = (UIImageView*)self.imageViewArrM[page];
//    if (self.tempImage==tableGridImage) {
//        return;
//    }
//
//    self.tempImage.alpha=0.6;
//    self.tempImage.layer.borderColor = [UIColor clearColor].CGColor;
//    self.tempImage.layer.borderWidth = 0.0;
//    self.tempImage=tableGridImage;
//    self.tempImage.alpha=1;
//    self.tempImage.layer.borderColor = [UIColor whiteColor].CGColor;
//    self.tempImage.layer.borderWidth = 1.0;
}

@end
